#include <jni.h>
#include <compressorjpeg.h>
#include <jnicallbackhelper.h>


JavaVM *jvm = 0;

jint JNI_OnLoad(JavaVM *vm, void *args) {
    ::jvm = vm;
    if (DEBUG)
        LOG_DEBUG("JNI_OnLoad success");
    return JNI_VERSION_1_6;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_bytecodezz_photoshop_compressor_CompressorTools_versionNative(JNIEnv *env, jobject thiz) {
    if (DEBUG) LOG_DEBUG("测试Native加载成功");
    // 测试 libturbojpeg.a 函数库导入是否成功
    jpeg_compress_struct jcs = jpeg_compress_struct();
    if (DEBUG)
        LOG_DEBUG("jpeg_compress_struct:jcs.image_width = %s jcs.image_height = %s ",
                  std::to_string(jcs.image_width).c_str(),
                  std::to_string(jcs.image_height).c_str());
}

extern "C"
JNIEXPORT void JNICALL
Java_com_bytecodezz_photoshop_compressor_CompressorTools_nativeCompressor(
        JNIEnv *env,
        jobject thiz,
        jobject bitmap,
        jint quality,
        jstring outPutPath_,
        jboolean open_child_thread) {

    jnicallbackhelper *helper = new jnicallbackhelper(jvm, env, thiz);

    const char *outPutPath = env->GetStringUTFChars(outPutPath_, nullptr);
    compressorjpeg *compressor = new compressorjpeg(bitmap, outPutPath, quality, helper);
    compressor->compressor(open_child_thread);

    env->ReleaseStringUTFChars(outPutPath_, outPutPath);
}